/*
Learning from Law Enforcement
Libor Dusek and Christian Traxler

Do-file producing regression results and figures in the paper and (online) appendix

*************************************
*** DESCRIPTION AND PRELIMINARIES
**************************************

*** This do file: RD_04.do
    Runs sensitivity analyses wrt parameters a and f (assignment / follow-up period) at the HIGH-FINE cutoff

*** Required packages:
    coefplot , grstyle , rdrobust

*** The input dataset:
    Dusek_Traxler_RD_a3.dta
    Dusek_Traxler_RD_a4.dta
    Dusek_Traxler_RD_a5.dta
    Dusek_Traxler_RD_a6.dta
	
*** Output is stored in output directories: OUTPUT/RD_TAB  OUTPUT/RD_FIG , and OUTPUT/RD_temp
	Names of outpupt files correspond to the Table/Figure numbers in the paper and (online) appendix
	
*** List of Figures produced:
    => Figure A.9
    See also Section C of the ReadMe File.
*/

cap clear
set more off

/* Set directories */
global dir C:/DATA/speeding_CZ/
cd    $dir
global datadir $dir/datasets/
global outputdir outfiles/
capture mkdir ${outputdir}
capture mkdir ${outputdir}/RD_TAB
capture mkdir ${outputdir}/RD_FIG
capture mkdir ${outputdir}/RD_temp

cap log close
log using    $dir/${outputdir}/log_RD_04.txt, replace

/* Load data with different Assignment Periods A (in months) */
forvalues a = 3(1)6 {

   global A = `a'
    di "assignment period (months):"
    di `a'

   /* Within each dta file, pick a follow-up (outcome) period 3<=f<=6 */
   forvalues f = 3(1)6 {
      di "outcome period (months):"
      di `f'
      global F = `f'

      cap clear
      use $datadir/Dusek_Traxler_RD_a${A}.dta

      rename period_follup_${F} period_follup
      drop period_follup_*
      rename period_follup period_post
      rename dum_haspost_${F} dum_haspost
      drop dum_haspost_*
      rename period_pre_${F} period_pre
      drop period_pre_*

      /* Keep only observations from pre & outcome period */
      qui keep if (period_pre==1 | period_post==1 | dum_trigger==1)

      /* only "bites" in A=4 data; other data are already cut */
      keep if day_trigger<td(1jul2017)

      /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      * II.A   DEFINE VARIABLES AND SAMPLE  [compare RD_02.do]
      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

      /* Defines assignment speed (cutoff 2) */
      gen speed_Z = maxspeed - 23

      /* focus on sample around Cutoff 1 */
      qui keep if speed_Z>-9 & speed_Z<20

      /* define first relevant episode (after sample restrictions) and cut sample */
      gegen epi_min=min(episode), by(id)
      keep if episode==epi_min
      drop epi_min

      /* Identifier for first relevant drive through in pre period */
      egen xxid_record_pre_min  = min(id_record) if (period_pre==1 | dum_trigger==1), by(id)
      egen id_record_pre_min  = mean(xxid_record_pre_min), by(id)
      drop xxid_record_*

      /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      * II.B   PRIMARY OUTCOME AND TREATMENT VARIABLES
      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

      /*High Fine Ticket Indicator */
      gen     xx_highfine=0
      replace xx_highfine=1 if dum_trigger==1 & violcat2==1
      by id: gegen high_fine = max(xx_highfine)
      gen dum_treated_high     = dum_ticketed     * high_fine

      /*TREATMENT RATE (2): share of rides after receiving HIGH FINE ticket */
      by id:  gegen xxticketed_m = mean(dum_treated_high) if period_post==1
      by id: gegen treated_high_m = mean(xxticketed_m)
      drop xx_* xxticketed*

      /*AVERAGE SPEED  (outcome var) */
      gegen xxspeedm_m    = mean(speedm) if period_post==1, by(id)
      gegen speedm_m    = mean(xxspeedm_m), by(id)
      drop xxspeedm_*

      /*SPEEDING RATE (outcome var), share of rides above speed limit */
      gegen xxspeeding_m     = mean(speeding) if period_post==1, by(id)
      gegen speeding_m     = mean(xxspeeding_m), by(id)
      drop xxspeeding_*

      /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      * II.C   WALD ESTIMATES (car level), Cutoff 2
      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
      gen     ESTSAMPLE=0
      replace ESTSAMPLE=1 if id_record==id_record_pre_min & dum_haspost==1

       rdrobust speedm_m speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
        estimates store speedm_a${A}_${F}

       rdrobust speeding_m  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
        estimates store speeding_a${A}_${F}
    }
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* II.D   Coefficient Plots => Figure A.9, (b) + (d)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

grstyle init
grstyle color background white


grstyle graphsize x 7
grstyle graphsize y 4

coefplot (speeding_a3_3, label(f = 3) pstyle(p3)) (speeding_a3_4, label(f = 4) pstyle(p4)) (speeding_a3_5, label(f = 5) pstyle(p5)) (speeding_a3_6, label(f = 6) pstyle(p6)), bylabel("a = 3") ///
      || (speeding_a4_3, label(f = 3) pstyle(p3)) (speeding_a4_4, label(f = 4) pstyle(p4)) (speeding_a4_5, label(f = 5) pstyle(p5)) (speeding_a4_6, label(f = 6) pstyle(p6)), bylabel("a = 4") ///
      || (speeding_a5_3, label(f = 3) pstyle(p3)) (speeding_a5_4, label(f = 4) pstyle(p4)) (speeding_a5_5, label(f = 5) pstyle(p5)) (speeding_a5_6, label(f = 6) pstyle(p6)), bylabel("a = 5") ///
      || (speeding_a6_3, label(f = 3) pstyle(p3)) (speeding_a6_4, label(f = 4) pstyle(p4)) (speeding_a6_5, label(f = 5) pstyle(p5)) (speeding_a6_6, label(f = 6) pstyle(p6)), bylabel("a = 6") ///
      || , drop(Conventional Bias-corrected) vert msymbol(D) rename(Robust = beta) byopts(compact cols(4)) subtitle(, size(medium)) legend(rows(1) size(medium) region(fcolor(none) lw(zero) lcolor(gs16)))
 graph export ${outputdir}/RD_FIG/Fig_A9_b_speeding.pdf, replace as(pdf)
 graph save   ${outputdir}/RD_FIG/Fig_A9_b_speeding.gph, replace


coefplot (speedm_a3_3, label(f = 3) pstyle(p3)) (speedm_a3_4, label(f = 4) pstyle(p4)) (speedm_a3_5, label(f = 5) pstyle(p5)) (speedm_a3_6, label(f = 6) pstyle(p6)), bylabel("a = 3") ///
      || (speedm_a4_3, label(f = 3) pstyle(p3)) (speedm_a4_4, label(f = 4) pstyle(p4)) (speedm_a4_5, label(f = 5) pstyle(p5)) (speedm_a4_6, label(f = 6) pstyle(p6)), bylabel("a = 4") ///
      || (speedm_a5_3, label(f = 3) pstyle(p3)) (speedm_a5_4, label(f = 4) pstyle(p4)) (speedm_a5_5, label(f = 5) pstyle(p5)) (speedm_a5_6, label(f = 6) pstyle(p6)), bylabel("a = 5") ///
      || (speedm_a6_3, label(f = 3) pstyle(p3)) (speedm_a6_4, label(f = 4) pstyle(p4)) (speedm_a6_5, label(f = 5) pstyle(p5)) (speedm_a6_6, label(f = 6) pstyle(p6)), bylabel("a = 6") ///
      || , drop(Conventional Bias-corrected) vert msymbol(D) rename(Robust = beta) byopts(compact cols(4)) subtitle(, size(medium)) legend(rows(1) size(medium) region(fcolor(none) lw(zero) lcolor(gs16)))
 graph export ${outputdir}/RD_FIG/Fig_A9_d_speed.pdf, replace as(pdf)
 graph save   ${outputdir}/RD_FIG/Fig_A9_d_speed.gph, replace


log close
